//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.IO;

namespace Ipc
{
    public class IpcClient<TData>
    {
        private bool disposed = false;
        IntPtr queueHandle = PointToPointMessageQueueHelper.INVALID_HANDLE;
        EventHandler<IpcEventArgs<TData>> eventHandler;

        public void Suscribe(String eventName, EventHandler<IpcEventArgs<TData>> eventHandler)
        {
            queueHandle = PointToPointMessageQueueHelper.CreateMessageQueue(eventName, true);

            this.eventHandler = eventHandler;

            new Thread(new ThreadStart(Worker)).Start();
        }

        public void Worker()
        {
            while (true)
            {
                PointToPointMessageQueueHelper.WaitForSingleObject(queueHandle, PointToPointMessageQueueHelper.TIMEOUT_INFINITE);

                if (eventHandler != null)
                {
                    byte[] buffer = new byte[1024];
                    PointToPointMessageQueueHelper.ReadMessageQueue(queueHandle,buffer);
                    
                    IpcEventArgs<TData> eventArgs = new IpcEventArgs<TData>();
                    XmlSerializer serializer = new XmlSerializer(typeof(TData));
                    MemoryStream memoryStream = new MemoryStream(buffer);
                    TData data = (TData) serializer.Deserialize(memoryStream);
                    eventArgs.Data = data;
                    eventHandler(this, eventArgs);
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                {
                }

                if (queueHandle != PointToPointMessageQueueHelper.INVALID_HANDLE)
                {
                    PointToPointMessageQueueHelper.CloseMessageQueue(queueHandle);

                    queueHandle = PointToPointMessageQueueHelper.INVALID_HANDLE;
                }
            }

            disposed = true;         
        }

        ~IpcClient()      
        {
            Dispose(false);
        }
    }
}
